import { userEnrollWebCastPage } from '../../../api/webCast'
import { collectTeachingResearch } from '../../../api/teachingResearch'

Page({
  data: {
    activeName: 'live',
    isLiveEnd: false,
    liveList: [],
    liveQuery: {
      condition: {},
      pager: {
        page: 1,
        pageSize: 10
      }
    },
    isActivityEnd: false,
    activityList: [],
    activityQuery: {
      condition: {},
      pager: {
        page: 1,
        pageSize: 10
      }
    }
  },

  onLoad: function (options) {
    this.getLiveList()
    this.getActivityList()
  },

  getLiveList() {
    if (!this.data.isLiveEnd) {
      userEnrollWebCastPage(this.data.liveQuery).then(res => {
        if (res.records.length) {
          let list = this.data.liveList
          list = list.concat(res.records)
          this.setData({
            liveList: list
          })
        }
        if (res.records.length < 10) {
          this.setData({
            isLiveEnd: true
          })
        }
      })
    }
  },

  getMoreLive() {
    const page = this.data.liveQuery.pager.page++
    this.setData({
      'liveQuery.pager.page': page
    })
    this.getLiveList()
  },

  getActivityList() {
    if (!this.data.isActivityEnd) {
      collectTeachingResearch(this.data.activityQuery).then(res => {
        if (res.records.length) {
          let list = this.data.activityList
          list = list.concat(res.records)
          this.setData({
            activityList: list
          })
        }
        if (res.records.length < 10) {
          this.setData({
            isActivityEnd: true
          })
        }
      })
    }
  },

  getMoreActivity() {
    const page = this.data.activityQuery.pager.page++
    this.setData({
      'activityQuery.pager.page': page
    })
    this.getActivityList()
  },

  tapTabs(e) {
    this.setData({
      activeName: e.detail.name
    })
  },

  toLower() {
    if (this.data.activeName === 'live') {
      this.getMoreLive()
    } else {
      this.getMoreActivity()
    }
  }
})
